# -*- coding: utf-8 -*-
# @Time : 2021/6/7 下午3:54
# @Author : fugang_le

import jieba
import numpy as np
from gensim.models import KeyedVectors

from app.libs.log_util import logger
from app.config import fasttext_vector_model
from app.libs.src.utils.util import spend_time

model = ""

@spend_time("load fasttext_vector_model")
def load_word2vec():
    logger.info("loading word2vec ......")
    global model
    model = KeyedVectors.load_word2vec_format(fasttext_vector_model)


def get_word_vector(word):
    try:
        vecs = model.get_vector(word)
    except Exception as ex:
        logger.error(ex)
        vecs = []
    return vecs

def get_content_vector(content):
    words = jieba.lcut(content)
    contents_vecs = np.array([0.0] * 300)
    count = 0
    for w in words:
        w_vecs = get_word_vector(w)
        if not w_vecs == []:
            contents_vecs += np.array(w_vecs)
            count += 1
        return contents_vecs / count




